//
//  SLHomeGuestCell.m
//  SlockApp
//
//  Created by 凯朱 on 2024/9/17.
//

#import "SLHomeGuestCell.h"
#import <SlockApp-Swift.h>
@interface SLHomeGuestCell ()

@property (nonatomic, strong)CAGradientLayer *yLayer;
@property (nonatomic, strong)CAGradientLayer *rLayer;
@property (nonatomic, strong)LottieAnimationView *animation;

@end

@implementation SLHomeGuestCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _yLayer = [self.topView addLandColorWithStart:KColor_O endColor:KColor_Y frame:CGRectMake(0, 0, kScreenWidth-40, 100)];
    _rLayer = [self.topView addLandColorWithStart:kHexColor(@"#DB1140") endColor:kHexColor(@"#DB1140") frame:CGRectMake(0, 0, kScreenWidth-40, 100)];
    [self.topView.layer insertSublayer:_rLayer below:_yLayer];
     _nameLbl.font = [UIFont boldSystemFontOfSize:22];
     _descLbl.font = kFont(12);
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.borderColor = kHexColor(@"#292929").CGColor;
    _bgView.layer.borderWidth = 1;
    
    kWeakSelf(self);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.btnBlock) {
            weakself.btnBlock(1, weakself.model);
        }
    }];
    _animationBgView.userInteractionEnabled = YES;
    [_animationBgView addGestureRecognizer:tap];
    _animationBgView.layer.cornerRadius = 25;
    _animationBgView.layer.masksToBounds = YES;
    _animation = [AnimationLoader loadAnimationFrom:@"LockIconUnlock_Y" into:self.animationView];
}

-(void)setModel:(SLLockModel *)model{
    _model = model;
    _nameLbl.text = _model.keyName;
    _descLbl.text = kLocalString(@"Guest");
    NSString *battery = [NSString stringWithFormat:@"%@%%",kReplace(_model.deviceElectricQty, @"-")];
    _batteryLbl.text = battery;
    NSString *startTime = [NSString convertDateString:_model.startTime outFormart:@"dd MMM. HH:mm"];
    NSString *endTime = [NSString convertDateString:_model.endTime outFormart:@"dd MMM. HH:mm"];
    NSString *timeStr = [NSString stringWithFormat:@"%@~%@",startTime,endTime];
   
    _bottomLblHeight.constant = 15;
    
    if (_model.keyType == 1) {
        _bottomLblHeight.constant = 5;
        _topLbl.text = kLocalString(@"Permenant access");
        _iconType.image = [UIImage imageNamed:@"icon_guest_forever"];
        _bottomLbl.text = @"";
    }else if (_model.keyType == 2){
        _topLbl.text = kLocalString(@"Duration access");
        _iconType.image = [UIImage imageNamed:@"icon_guest_dur"];
        _bottomLbl.text = timeStr;
    }else if (_model.keyType == 3){
        _topLbl.text = kLocalString(@"One-time access");
        _iconType.image = [UIImage imageNamed:@"icon_guest_one"];
        _bottomLbl.text = timeStr;
    }
    
}

-(void)playUlockAnimation:(void(^)(void))finish{
    [AnimationLoader playAnimationAndStopWithAnimationView:_animation for:2 completion:finish];
}

@end
